package com.huage_10.lesson_02;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectDemo03 {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        //获取Class对象
        Class<?> c = Class.forName("com.huage_10.lesson_02.Student");

        //获取方法
//        Method[] methods = c.getMethods();
//        Method[] methods = c.getDeclaredMethods();
//        for (Method method : methods){
//            System.out.println(method);
//        }

        //调用方法
        Object obj = c.newInstance();
        Method method1 = c.getMethod("method1");
        method1.invoke(obj);
        System.out.println("--------");

        Method method2 = c.getDeclaredMethod("method2");
        method2.setAccessible(true);
        method2.invoke(obj);
        System.out.println("--------");

        Method method3 = c.getDeclaredMethod("method3");
        method3.setAccessible(true);
        method3.invoke(obj);
        System.out.println("--------");

        Method method4 = c.getDeclaredMethod("method4", int.class, String.class);
        method4.setAccessible(true);
        Object o = method4.invoke(obj,4,"方法");
        System.out.println(o);
        System.out.println("--------");

    }
}
